<template>
  <div>
    <eagle-dialog-page
      :params="paramArr"
      @initList="bindData"
      @open="open"
      @submit="submit"
      @callbackDialog="callbackDialog"
      @setData="setData"
      v-bind="$attrs"
      v-on="$listeners"
      ref="eagleDialogPage"
      :tab="true"
      :isPage="true"
      :isMulti="multi"
      :option="option"
      @onOpen="onOpen"
    >
      <eagle-cell
        v-for="dataItem in list"
        :key="dataItem.AreaCode"
        :title="dataItem.AreaName"
        :label="dataItem.Remarks || '暂无描述'"
      >
        <template #right-icon
          ><eagle-checkbox :name="dataItem.AreaCode" shape="round"
        /></template>
      </eagle-cell>

      <template #seletedSlot>
        <van-cell
          name="seletedSlot"
          v-for="(item, index) in multiselectedlist"
          :key="index"
        >
          <div class="risk_item_buton">
            <div class="mb-5">
              <div class="mb-5">{{ item.AreaName }}</div>
              <div>
                {{ item.AreaName }}
              </div>
            </div>
            <van-icon name="delete" @click="deleteSelect(item.AreaCode)" />
          </div>
        </van-cell>
      </template>
    </eagle-dialog-page>
  </div>
</template>
<script>
import { deepClone } from "@/utils";
import store from "@/store/index";
export default {
  props: {
    title: {
      type: String,
      default: "选择检查点",
    },
    confirmButton: {
      type: String,
      default: "完成",
    },
    tagData: {
      type: Array,
      default: () => [],
    },
    multi: {
      type: Boolean,
      default: () => true,
    },
    onlyRiskUnit: {
      type: Boolean,
      default: () => false,
    },
  },
  data() {
    return {
      dangerAreaCustomType: "",
      paramArr: [],
      multiselectedlist: this.tagData,
      selecteData: [],
      option: { name: "AreaName", code: "AreaCode" },
      list: [],
    };
  },
  created() {},
  methods: {
    loadParams() {
      this.paramArr = [];
      if (this.dangerAreaCustomType.indexOf("A") >= 0) {
        this.paramArr.push({
          url: "/RiskUnit/GetPageData",
          title: "检查点[风险位置]",
          Name: "Keywords",
          dataType: "DangerHiddenArea",
          page: -1,
          limit: 10,
        });
      } else if (
        this.dangerAreaCustomType.indexOf("B") >= 0 &&
        !this.onlyRiskUnit
      ) {
        this.paramArr.push({
          url: "/RiskUnitArea/GetPageData",
          title: "检查点[风险区域]",
          Name: "Keywords",
          dataType: "DangerHiddenArea",
          page: -1,
          limit: 10,
        });
      }
      if (
        this.dangerAreaCustomType.indexOf("C") >= 0 &&
        store.state.hasRiskControl &&
        !store.state.isFW &&
        !store.state.IsJGB
      ) {
        this.paramArr.push({
          url: "/RiskPoint/GetPageData",
          title: "检查点[风险点]",
          Name: "Keywords",
          dataType: "DangerHiddenArea",
          page: -1,
          limit: 10,
        });
      }
      if (this.dangerAreaCustomType.indexOf("D") >= 0) {
        this.paramArr.push({
          url: "/UserOrg/GetPageData",
          title: "检查点[部门/班组]",
          Name: "Keywords",
          dataType: "DangerHiddenArea",
          page: -1,
          limit: 10,
        });
      }
      if (
        this.dangerAreaCustomType.indexOf("D") >= 0 &&
        store.state.hasEquCatalog &&
        !store.state.isFW &&
        !store.state.IsJGB
      ) {
        this.paramArr.push({
          url: "/EquEquipment/GetPageData",
          title: "检查点[设施设备]",
          Name: "Keywords",
          dataType: "DangerHiddenArea",
          page: -1,
          limit: 10,
        });
      }
      if (store.state.hasLesseeCatalog) {
        this.paramArr.push({
          url: "/RentalPlace/GetPageData",
          title: "检查点[出租场所]",
          Name: "Keywords",
          dataType: "DangerHiddenArea",
          page: -1,
          limit: 10,
        });
      }
    },
    getOptions() {
      this.$axios
        .get("/ParamOption/GetOptionKeyValue", {
          params: { key: "DangerAreaCustomType" },
        })
        .then((res) => {
          if (res.success) {
            this.dangerAreaCustomType = res.data || "";
            this.loadParams();
          }
        });
    },
    bindData(list) {
      this.list = list;
      this.multiselectedlist = this.tagData;
      this.$refs.eagleDialogPage.initSeletedData(this.multiselectedlist);
    },
    open(list) {
      this.listUser = list;
    },
    submit() {
      this.$refs.eagleDialogPage.submit();
    },
    setData(data) {
      if (this.multi) {
        this.multiselectedlist = data;
      } else {
        this.simSelectObject = data;
      }
    },
    callbackDialog(data) {
      if (!data || data.length == 0) {
        this.$toast("您还未选择人员");
        return false;
      }
      if (this.multi) {
        //处理数据与pc保持一致
        let resultArr = [];
        resultArr = deepClone(this.multiselectedlist);
        if (resultArr && resultArr.length > 0) {
          for (var i = 0; i < resultArr.length; i++) {
            let item = this.multiselectedlist[i];
            item.CheckArea = item.AreaName;
            item.CheckAreaCode = item.AreaCode;
            item.CheckAreaType = item.AreaType;
            item.CheckObjectsType = "2";
          }
        }

        this.$emit("callback", resultArr);
      } else {
        this.$emit("callback", { code: data.code, name: data.name });
      }
    },
    deleteSelect(code) {
      this.$refs.eagleDialogPage.deleteSelect(code);
    }
  },
};
</script>